﻿using AutoMapper;
using Common.Damin;
using Common.Infrastructrue.Interface;
using Common.Service;
using MediatR;
using RabitPart.WritedPartApi.Application.Comman.CultivationFile;

namespace RabitPart.WritedPartApi.Application.CommanHandel.CultivationFile
{
    /// <summary>
    /// 养殖企业信息
    /// </summary>
    public class CultivationcorporateCommanHandel : IRequestHandler<CultivationcorporateComman, ApiResult>
    {
        private readonly ICultivationcorporateResponstity cultivationcorporateRE;
        private readonly IMapper mapper;
        private readonly ILogger<CultivationcorporateCommanHandel> logger;
        public CultivationcorporateCommanHandel(ICultivationcorporateResponstity cultivationcorporateRE, IMapper mapper, ILogger<CultivationcorporateCommanHandel> logger)
        {
            this.cultivationcorporateRE = cultivationcorporateRE;
            this.mapper = mapper;
            this.logger = logger;
        }
        public Task<ApiResult> Handle(CultivationcorporateComman request, CancellationToken cancellationToken)
        {
            try
            {
                var Cultivationcorporate = mapper.Map<Cultivationcorporate>(request);
                cultivationcorporateRE.InsertAsync(Cultivationcorporate);

                return Task.FromResult(ApiResult.Success(StatusCodeEnum.Ok));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "CultivationCommanHandel");
                return Task.FromResult(ApiResult.Fail(ex.Message, StatusCodeEnum.Fail));
            }
        }
    }
}
